﻿/**********************************弘石项目*******************************************
* 功能描述:项目管理
* 作者:石锦涛
* 时间:2021-12-7
* 
**********************************弘石项目*******************************************/
using HongStone.Domain.OperationsManagement;
using HongStone.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MiniExcelLibs;

namespace HongStone.API.Controllers
{
    [ApiExplorerSettings(GroupName = "Operation")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OperationController : ControllerBase
    {
        private readonly OperationBll _operationBll;
        public OperationController(OperationBll operationBll)
        {
            _operationBll = operationBll;
        }

        /// <summary>
        /// 字典下拉框数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Showdictionaries()
        {
            try
            {
                return Ok(_operationBll.Showdictionaries());
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        ///  合同关联
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowContract( string cname, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var totalCount = 0;
                var list = _operationBll.ShowContract(out totalCount, cname, pageindex, pagesize);
                return Ok(new { totalCount, list });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 承接部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowCJSection( int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var totalCount = 0;
                var list = _operationBll.ShowCJSection(out totalCount, pageindex, pagesize);
                return Ok(new { totalCount,list});
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 甲方负责人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowFirstParty(string xname, string bumen, string phone, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var  totalCount = 0;
                var list = _operationBll.ShowFirstParty(out totalCount, xname, bumen, phone, pageindex, pagesize);
                return Ok(new { totalCount, list });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 执行设总
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowSZExecute(string zname, int bumen, string phone, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var totalCount = 0;
                var list = _operationBll.ShowSZExecute(out totalCount, zname, bumen, phone, pageindex, pagesize);
                return Ok(new { totalCount, list });

            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 上传 保存图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpLoad()
        {
            //http 协议 Request Response
            //获取文件
            var hpf = Request.Form.Files[0];
            //新名称
            var newFileName = Guid.NewGuid().ToString();
            //获取扩展名
            var ext = Path.GetExtension(hpf.FileName);
            //将文件名和扩展名拼接
            var newName = newFileName + ext;
            //找到路径  将虚拟路径转成物理路径
            var path = Directory.GetCurrentDirectory() +"/Images";
            //合并路径和文件名
            var newPath = Path.Combine(path, newName);

            using (var stream = System.IO.File.Create(newPath))
            {
                 hpf.CopyTo(stream);

            }
            return Ok(new { url = newName });
        }

        /// <summary>
        /// 立项结构样式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowStructure()
        {
            try
            {
                return Ok(_operationBll.ShowStructure());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 建筑分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowArchitecture()
        {
            try
            {
                return Ok(_operationBll.ShowArchitecture());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 添加项目立项
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddProjectLX(Project project)
        {
            try
            {
                return Ok(_operationBll.AddProjectLX(project));
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 显示项目信息
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="bumen"></param>
        /// <param name="xmName"></param>
        /// <param name="jsClass"></param>
        /// <param name="jsgm"></param>
        /// <param name="jsgms"></param>
        /// <param name="jsdanwei"></param>
        /// <param name="jgxs"></param>
        /// <param name="jzflei"></param>
        /// <param name="zxsez"></param>
        /// <param name="jiafz"></param>
        /// <param name="hte"></param>
        /// <param name="htes"></param>
        /// <param name="didian"></param>
        /// <param name="xinzhi"></param>
        /// <param name="laiyuan"></param>
        /// <param name="begtime"></param>
        /// <param name="begtimes"></param>
        /// <param name="endtime"></param>
        /// <param name="endtimes"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowProject(int? bumen,string xmName,int? jsClass,decimal? jsgm, decimal? jsgms, string jsdanwei,string jgxs,string jzflei,string zxsez,string jiafz,decimal? hte, decimal? htes, string didian,int? xinzhi,int? laiyuan,DateTime? begtime, DateTime? begtimes, DateTime? endtime, DateTime? endtimes, int pageindex=1,int pagesize=10)
        {
            try
            {
                var totalCount = 0;
                var list= _operationBll.ShowProject(out totalCount, bumen, xmName, jsClass, jsgm, jsgms, jsdanwei, jgxs, jzflei, zxsez, jiafz, hte, htes, didian, xinzhi, laiyuan, begtime, begtimes, endtime, endtimes, pageindex, pagesize);
                return Ok(new { totalCount, list });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        ///  导出 Excel 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcel()
        {

            var totalCount = 0;
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _operationBll.ShowProject(out totalCount,0, "", 0, 0, 0, "", "", "", "", "", 0, 0, "", 0, 0, null, null, null, null, 1, 10);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.Data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeletProject(string pid)
        {
            try
            {
                return Ok(_operationBll.DeletProject(pid));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult BianJi(int id)
        {
            try
            {
                return Ok(_operationBll.BianJi(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 详情导出
        /// </summary>
        /// <param name="idc"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DaochuXQ(int idc)
        {
            try
            {

                return Ok(_operationBll.DaochuXQ(idc));

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        ///  导出 Excel 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult BianloadExcel()
        {
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _operationBll.DaochuXQ(0);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.Data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }

        /// <summary>
        /// 项目信息修改
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ProjeUpdate(Project project)
        {
            try
            {
                return Ok(_operationBll.ProjeUpdate(project));
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 显示项目关联合同
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="bumen"></param>
        /// <param name="xmName"></param>
        /// <param name="endtime"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowContractLinkage( int? bumen, string xmName, DateTime? rtime, DateTime? endtime, int pageindex = 1, int pagesize = 10)
        {
            try
            {
                var totalCount = 0;
                var list = _operationBll.ShowContractLinkage(out totalCount, bumen, xmName, rtime, endtime, pageindex, pagesize);
                return Ok(new { totalCount, list });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示项目关联合同 导出 Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowContractExcel()
        {

            var totalCount = 0;
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _operationBll.ShowContractLinkage(out totalCount,0,null,null, null, 1,10);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.Data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }


        /// <summary>
        /// 承接部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Showbumen()
        {
            try
            {
                return Ok(_operationBll.Showbumen());
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 部门 项目统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowStatistics(int bu)
        {
            try
            {
                return Ok(_operationBll.ShowStatistics(bu));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 项目 季度 统计
        /// </summary>
        /// <param name="bu"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowQuarter(int bu)
        {
            try
            {

                return Ok(_operationBll.ShowQuarter(bu));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 项目 月度 统计
        /// </summary>
        /// <param name="bu"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Showmonthly(int bu)
        {
            try
            {

                return Ok(_operationBll.Showmonthly(bu));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 项目统计 导出 Excel 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadQuarter()
        {
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _operationBll.ShowQuarter(0);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.ToList());
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }

        /// <summary>
        /// 项目进度统计
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="bumen"></param>
        /// <param name="xmName"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowSchedule(int? bumen, string xmName, int pageindex = 1, int pagesize = 10)
        {
            try
            {
                var totalCount = 0;
                var list = _operationBll.ShowSchedule(out totalCount, bumen, xmName, pageindex, pagesize);
                return Ok(new { totalCount,list});
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 项目进度统计导出 Excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StatisticsExcel()
        {

            var totalCount = 0;
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _operationBll.ShowSchedule(out totalCount,0,null, 1, 10);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.Data);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }


        /// <summary>
        /// 人员展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowMember()
        {
            try
            {
                return Ok(_operationBll.ShowMember());
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
